//
//  LVRefreshFooter.m
//  Live
//
//  Created by 熙文 张 on 16/5/17.
//  Copyright © 2016年 Heller. All rights reserved.
//

#import "LVRefreshFooter.h"
#import "LSYNLineView.h"
#import "BNDOfficeDocKit.h"

@interface LVRefreshFooter()

@property (strong, nonatomic) UILabel *stateNoMoreLabel;
@property (strong, nonatomic) UIView  *loadingView;
@property (strong, nonatomic) UILabel *stateLabel;
@property (strong, nonatomic) UILabel *stateIdleLabel;
@property (strong, nonatomic) UIActivityIndicatorView *refreshImageView;

@property (strong, nonatomic) UIView *leftLine;
@property (strong, nonatomic) UIView *rightLine;

@end

@implementation LVRefreshFooter

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    self.mj_w = YYScreenSize().width;
    
    self.loadingView = [UIView new];
    [self addSubview:self.loadingView];
    
    self.refreshImageView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhite)];
    [self.refreshImageView setColor:UIColorHex(0x6d6d7b)];
    self.refreshImageView.contentMode = UIViewContentModeCenter;
    [self.refreshImageView startAnimating];
    [self.loadingView addSubview:self.refreshImageView];
    
    self.stateLabel = [UILabel new];
    self.stateLabel.text = @"正在加载";
    self.stateLabel.textAlignment = NSTextAlignmentCenter;
    self.stateLabel.backgroundColor = [UIColor clearColor];
    self.stateLabel.font = BNDPFRegularFont(14);
    self.stateLabel.textColor = UIColorHex(0x828292);
    self.stateLabel.enabled = NO;
    [self.loadingView addSubview:self.stateLabel];
    
    [self addSubview:self.stateNoMoreLabel];
    [self addSubview:self.stateIdleLabel];
}

#pragma mark - Getter

- (UILabel *)stateIdleLabel
{
    if (!_stateIdleLabel) {
        _stateIdleLabel = [UILabel new];
        _stateIdleLabel.text = @"上拉加载更多数据";
        _stateIdleLabel.textAlignment = NSTextAlignmentCenter;
        _stateIdleLabel.backgroundColor = [UIColor clearColor];
        _stateIdleLabel.font = BNDPFRegularFont(14);
        _stateIdleLabel.textColor = UIColorHex(0x828292);
        _stateIdleLabel.enabled = NO;
        [_stateIdleLabel setHidden:YES];
    }
    return _stateIdleLabel;
}

- (UILabel *)stateNoMoreLabel
{
    if (!_stateNoMoreLabel) {
        _stateNoMoreLabel = [UILabel new];
        _stateNoMoreLabel.text = @"这里已经是底线了";
        _stateNoMoreLabel.textAlignment = NSTextAlignmentCenter;
        _stateNoMoreLabel.backgroundColor = [UIColor clearColor];
        _stateNoMoreLabel.font = BNDPFRegularFont(14);
        _stateNoMoreLabel.textColor = UIColorHex(0x828292);
        _stateNoMoreLabel.enabled = NO;
        _stateNoMoreLabel.hidden = YES;
        [_stateNoMoreLabel sizeToFit];
        
        [_stateNoMoreLabel setFrame:CGRectMake((self.width - _stateNoMoreLabel.width)/2,
                                               (self.height - _stateNoMoreLabel.height)/2,
                                               _stateNoMoreLabel.width,
                                               _stateNoMoreLabel.height)];
    }
    return _stateNoMoreLabel;
}

- (UIView *)leftLine
{
    if (!_leftLine) {
        
        _leftLine = [[LSYNLineView alloc] initGrayLineWithFrame:CGRectMake(0, 0, 100, 1) vertical:NO isFirstPixelOpaque:YES];
        [self addSubview:_leftLine];
        
        [_leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(25);
            make.right.mas_equalTo(self.stateNoMoreLabel.mas_left).offset(-15);
            make.height.mas_equalTo(1);
            make.centerY.mas_equalTo(self.stateNoMoreLabel.mas_centerY);
        }];
    }
    
    return _leftLine;
}

- (UIView *)rightLine
{
    if (!_rightLine) {
        
        _rightLine = [[LSYNLineView alloc] initGrayLineWithFrame:CGRectMake(0, 0, 100, 1) vertical:NO isFirstPixelOpaque:YES];
        [self addSubview:_rightLine];
        
        [_rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.stateNoMoreLabel.mas_right).offset(15);
            make.right.mas_equalTo(self.mas_right).offset(-25);
            make.height.mas_equalTo(1);
            make.centerY.mas_equalTo(self.stateNoMoreLabel.mas_centerY);
        }];
    }
    
    return _rightLine;
}

#pragma mark - 在这里设置子控件的位置和尺寸

- (void)placeSubviews
{
    [super placeSubviews];
    
    [self.stateNoMoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.size.mas_equalTo(self.stateNoMoreLabel.size);
    }];
    
    [self.stateIdleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.refreshImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.loadingView.mas_centerY);
        make.left.mas_equalTo(0);
    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.loadingView.mas_centerY);
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(self.refreshImageView.mas_right).with.offset(10);
    }];
}

#pragma mark - 监听控件的刷新状态

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    self.stateIdleLabel.hidden = YES;
    self.loadingView.hidden = YES;
    
    self.leftLine.hidden = YES;
    self.rightLine.hidden = YES;
    self.stateNoMoreLabel.hidden = YES;

    switch (state) {
        case MJRefreshStateIdle:
            break;
        case MJRefreshStateRefreshing:
            [self.loadingView setHidden:NO];
            break;
        case MJRefreshStateNoMoreData:
            self.stateNoMoreLabel.hidden = NO;
            self.leftLine.hidden = NO;
            self.rightLine.hidden = NO;
            break;
        default:
            break;
    }
}

- (void)startAnimating
{
    [self beginRefreshing];
}

- (void)stopAnimating
{
    [self endRefreshing];
}

@end
